package com.woniushop.dao.imple;

import java.util.List;

import com.woniushop.dao.BaseDAO;
import com.woniushop.dao.GoodsDAO;
import com.woniushop.entity.Goods;

/**
 * 商品DAO的实现类
 * 
 * @author 崔新雄
 *
 */
public class GoodsDAOImple implements GoodsDAO {

	private BaseDAO<Goods> baseDAO = new BaseDAO<Goods>();

	/**
	 * 将goods对象加入数据库中
	 * 
	 * @param goods
	 */
	@Override
	public void add(Goods goods) {
		String sql = "insert into goods(uid,gtid,gname,gdesc,gprice,gpic,gstock,gstate) values(?,?,?,?,?,?,?,?)";
		Object[] objs = { goods.getUid(), goods.getGtid(), goods.getGname(), goods.getGdesc(), goods.getGprice(),
				goods.getGpic(), goods.getGstock(), goods.getGstate() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据商品id删除数据库中对应的商品数据
	 * 
	 * @param gid
	 */
	@Override
	public void delete(Integer gid) {
		String sql = "delete from goods where gid=?";
		Object[] objs = { gid };
		baseDAO.update(sql, objs);
	}

	/**
	 * 修改对应的商品数据
	 * 
	 * @param goods
	 */
	@Override
	public void update(Goods goods) {
		String sql = "update goods set uid=?,gtid=?,gname=?,gdesc=?,gprice=?,gpic=?,gstock=?,gstate=? where gid=?";
		Object[] objs = { goods.getUid(), goods.getGtid(), goods.getGname(), goods.getGdesc(), goods.getGprice(),
				goods.getGpic(), goods.getGstock(), goods.getGstate(), goods.getGid() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据对应的商品id查找商品数据
	 * 
	 * @param gid
	 * @return
	 */
	@Override
	public Goods findOne(Integer gid) {
		String sql = "select g.*,gt.gtname,u.uname,u.uaccount from goods g,goodstype gt,user u where g.gtid=gt.gtid and g.uid=u.uid and gid=?";
		Object[] objs = { gid };
		List<Goods> list = baseDAO.select(sql, Goods.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 多条件分页查询商品数据
	 * 
	 * @param page
	 * @param pageSize
	 * @param goods
	 * @return
	 */
	@Override
	public List<Goods> findByPage(Integer page, Integer pageSize, Goods goods) {
		String sql = "select g.*,gt.gtname,u.uname,u.uaccount from goods g,goodstype gt,user u where g.gtid=gt.gtid and g.uid=u.uid";
		sql = finishSQL(goods, sql);
		sql += " limit ?,?";
		return baseDAO.select(sql, Goods.class, (page - 1) * pageSize, pageSize);
	}

	/**
	 * 查找符合条件的商品数量
	 * 
	 * @param goods
	 * @return
	 */
	@Override
	public Integer count(Goods goods) {
		String sql = "select count(*) from goods g,goodstype gt,user u where g.gtid=gt.gtid and g.uid=u.uid";
		sql = finishSQL(goods, sql);
		return baseDAO.count(sql);
	}

	/**
	 * 完成多条件查询的SQl语句
	 * 
	 * @param goods
	 * @param sql
	 * @return
	 */
	private String finishSQL(Goods goods, String sql) {
		String gdesc = goods.getGdesc();
		String gname = goods.getGname();
		if (gdesc != null && gdesc.equals(gname)) {
			sql += " and ( gdesc like '%" + gdesc + "%' or gname like '%\" + gname + \"%' ) ";
		} else {
			if (gdesc != null && !gdesc.trim().equals("")) {
				sql += " and gdesc like '%" + gdesc + "%'";
			}
			if (gname != null && !gname.trim().equals("")) {
				sql += " and gname like '%" + gname + "%'";
			}
		}
		Integer gstate = goods.getGstate();
		if (gstate != null) {
			sql += " and  gstate='" + gstate + "'";
		}
		Integer gtid = goods.getGtid();
		if (gtid != null) {
			sql += " and  g.gtid='" + gtid + "'";
		}
		Integer uid = goods.getUid();
		if (uid != null) {
			sql += " and  g.uid='" + uid + "'";
		}
		return sql;
	}

}
